// 客户案例页面交互逻辑
document.addEventListener('DOMContentLoaded', function() {
    // 导航栏滚动效果
    window.addEventListener('scroll', function() {
        const navbar = document.querySelector('.navbar');
        if (window.scrollY > 50) {
            navbar.classList.add('bg-white', 'shadow-md');
            navbar.classList.remove('bg-transparent');
        } else {
            navbar.classList.add('bg-white');
            navbar.classList.remove('shadow-md', 'bg-transparent');
        }
    });

    // 处理关于我们的弹窗交互
    const aboutUsBtn = document.getElementById('aboutUsBtn');
    const aboutUsDropdown = document.getElementById('aboutUsDropdown');

    if (aboutUsBtn && aboutUsDropdown) {
        aboutUsBtn.addEventListener('click', function(e) {
            e.preventDefault();
            aboutUsDropdown.classList.toggle('show');
        });
        
        // 点击其他区域关闭下拉菜单
        document.addEventListener('click', function(e) {
            if (aboutUsBtn && aboutUsDropdown && !aboutUsBtn.contains(e.target) && !aboutUsDropdown.contains(e.target)) {
                aboutUsDropdown.classList.remove('show');
            }
        });
        
        // 为下拉菜单中的荣誉证书和合作伙伴按钮添加点击事件
        const dropdownItems = document.querySelectorAll('#aboutUsDropdown .dropdown-item');
        dropdownItems.forEach(item => {
            item.addEventListener('click', function() {
                if (this.textContent.trim() === '合作伙伴') {
                    window.location.href = 'certificates.html?type=partners';
                } else if (this.textContent.trim() === '荣誉证书') {
                    window.location.href = 'certificates.html?type=certificates';
                } else if (this.textContent.trim() === '公司简介') {
                    window.location.href = 'about.html';
                }
            });
        });
    }

    // 替换案例页面图片为partner系列图片的函数
    function replaceCasePageImages() {
        // 替换案例列表中的图片
        const caseImages = document.querySelectorAll('.case-item img');
        const partnerImageNames = [
            'partner1.png', 'partner2.png', 'partner3.png', 'partner4.png',
            'partner5.png', 'partner6.png', 'partner7.png', 'partner8.png'
        ];
        
        caseImages.forEach((img, index) => {
            if (index < partnerImageNames.length) {
                img.src = `./static/image/${partnerImageNames[index]}`;
                img.alt = `合作伙伴${index + 1}`;
            }
        });
    }

    // 移动端导航菜单控制
    const navbarToggler = document.querySelector('.navbar-toggler');
    const navbarNav = document.querySelector('#navbarNav');
    
    navbarToggler.addEventListener('click', function() {
        navbarNav.classList.toggle('show');
    });

    // 移动端导航菜单关闭处理
    document.querySelectorAll('.nav-link, .dropdown-item').forEach(link => {
        link.addEventListener('click', function() {
            const navbarCollapse = document.getElementById('navbarNav');
            if (navbarCollapse && navbarCollapse.classList.contains('show')) {
                const navbarToggler = document.querySelector('.navbar-toggler');
                if (navbarToggler) {
                    navbarToggler.click();
                }
            }
        });
    });

    // 案例筛选功能
    const filterBtns = document.querySelectorAll('.filter-btn');
    
    filterBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            // 移除所有按钮的active类
            filterBtns.forEach(b => b.classList.remove('active'));
            // 为当前点击的按钮添加active类
            this.classList.add('active');
            
            // 获取筛选类型
            const filterType = this.textContent.trim();
            console.log(`筛选类型: ${filterType}`);
            
            // 获取所有案例项
            const caseItems = document.querySelectorAll('.case-item');
            
            // 根据筛选类型显示或隐藏案例
            caseItems.forEach((item, index) => {
                // 先添加淡出动画
                item.style.opacity = '0';
                item.style.transform = 'scale(0.95)';
                item.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
                
                // 延迟后根据筛选类型决定是否显示
                setTimeout(() => {
                    if (filterType === '全部' || index % 2 === 0) { // 简单的模拟筛选逻辑
                        item.style.display = 'block';
                        // 添加淡入动画
                        setTimeout(() => {
                            item.style.opacity = '1';
                            item.style.transform = 'scale(1)';
                        }, 50);
                    } else {
                        item.style.display = 'none';
                    }
                }, 300);
            });
        });
    });
    
    // 案例项点击效果增强
    const caseItems = document.querySelectorAll('.case-item');
    
    caseItems.forEach(item => {
        item.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-5px)';
            this.style.boxShadow = '0 10px 20px rgba(0, 0, 0, 0.1)';
            this.style.transition = 'transform 0.3s ease, box-shadow 0.3s ease';
        });
        
        item.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
            this.style.boxShadow = 'none';
            this.style.transition = 'transform 0.3s ease, box-shadow 0.3s ease';
        });
        
        // 点击跳转到详情页
        item.addEventListener('click', function() {
            const caseId = this.getAttribute('data-case-id');
            if (caseId) {
                window.location.href = `case-detail.html?id=${caseId}`;
            }
        });
    });
    
    // 分页控制功能
    const paginationBtns = document.querySelectorAll('.pagination-btn:not(.prev-btn):not(.next-btn)');
    const prevBtn = document.querySelector('.prev-btn');
    const nextBtn = document.querySelector('.next-btn');
    let currentPage = 1;
    
    // 分页按钮点击事件
    paginationBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            // 移除所有分页按钮的active类
            paginationBtns.forEach(b => b.classList.remove('active'));
            // 给当前按钮添加active类
            this.classList.add('active');
            
            // 更新当前页码
            currentPage = parseInt(this.textContent);
            console.log(`当前页码: ${currentPage}`);
            
            // 更新上一页/下一页按钮状态
            updatePaginationButtons();
            
            // 模拟页面切换效果
            simulatePageChange();
        });
    });
    
    // 上一页按钮点击事件
    prevBtn.addEventListener('click', function() {
        if (this.classList.contains('disabled')) return;
        
        if (currentPage > 1) {
            currentPage--;
            
            // 更新页码按钮状态
            updatePageButtonActiveState();
            // 更新前后按钮状态
            updatePaginationButtons();
            // 模拟页码切换效果
            simulatePageChange();
        }
    });
    
    // 下一页按钮点击事件
    nextBtn.addEventListener('click', function() {
        if (this.classList.contains('disabled')) return;
        
        if (currentPage < 5) { // 假设总共有5页
            currentPage++;
            
            // 更新页码按钮状态
            updatePageButtonActiveState();
            // 更新前后按钮状态
            updatePaginationButtons();
            // 模拟页码切换效果
            simulatePageChange();
        }
    });
    
    // 更新页码按钮的active状态
    function updatePageButtonActiveState() {
        paginationBtns.forEach(btn => {
            if (parseInt(btn.textContent) === currentPage) {
                btn.classList.add('active');
            } else {
                btn.classList.remove('active');
            }
        });
    }
    
    // 更新前后按钮状态
    function updatePaginationButtons() {
        // 第一页时禁用上一页按钮
        if (currentPage === 1) {
            prevBtn.classList.add('disabled');
        } else {
            prevBtn.classList.remove('disabled');
        }
        
        // 最后一页时禁用下一页按钮
        if (currentPage === 5) { // 假设总共有5页
            nextBtn.classList.add('disabled');
        } else {
            nextBtn.classList.remove('disabled');
        }
    }
    
    // 模拟页码切换效果
    function simulatePageChange() {
        const caseItems = document.querySelectorAll('.case-item');
        
        // 先隐藏所有案例
        caseItems.forEach(item => {
            item.style.opacity = '0';
            item.style.transform = 'scale(0.9)';
            item.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
        });
        
        // 然后根据当前页码显示相应的案例
        setTimeout(() => {
            caseItems.forEach((item, index) => {
                // 简单的模拟分页逻辑，每页显示4个案例
                const startIndex = (currentPage - 1) * 4;
                const endIndex = startIndex + 4;
                
                if (index >= startIndex && index < endIndex) {
                    item.style.display = 'block';
                    // 添加淡入动画
                    setTimeout(() => {
                        item.style.opacity = '1';
                        item.style.transform = 'scale(1)';
                    }, 50);
                } else {
                    item.style.display = 'none';
                }
            });
        }, 300);
    }
    
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            
            const targetId = this.getAttribute('href');
            if (targetId === '#') return;
            
            const targetElement = document.querySelector(targetId);
            if (targetElement) {
                window.scrollTo({
                    top: targetElement.offsetTop - 80,
                    behavior: 'smooth'
                });
            }
        });
    });
    
    // 检查URL参数，如果是从合作伙伴链接进入，执行图片替换
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('type') && urlParams.get('type') === 'partners') {
        replaceCasePageImages();
    }
});